#============================================================
#	Physics Layer
#============================================================
#  对应层的值
#============================================================
# @datetime: 2022-5-14 22:17:06
#============================================================
extends Node


var Layer : _Layer = _Layer.new()


class _Layer:
	var Role : int 
	var Player : int 
	var Enemy : int 
	var Item : int 
	
	var Env : int 
	var Floor : int 
	var Stairs : int 
	var Trap : int 
	var Bridge : int 
	
	var _data := {}
	
	func get_layer_keys():
		return _data.keys()
	
	func get_data():
		for p_data in get_script().get_script_property_list():
			_data[p_data['name']] = get(p_data['name'])
		_data.erase("_data")
		return _data


#============================================================
#   内置
#============================================================
func _init():
	for i in range(1, 32 + 1):
		var layer_name : String = ProjectSettings.get("layer_names/2d_physics/layer_%d" % i)
		if layer_name in Layer:
			Layer.set(layer_name, 1 << i - 1)

func _ready():
	Logger.info(self, [
		"所有的碰撞层：\n\t\t", Layer.get_layer_keys()
	])


#============================================================
#   自定义
#============================================================
##  是否在这个碰撞层中
static func is_in_layer(node: CollisionObject2D, layer: int) -> bool:
	return node.collision_layer & layer == layer

##  是否在这个碰撞掩码中
static func is_in_mask(node: CollisionObject2D, mask: int) -> bool:
	return node.collision_mask & mask == mask

##  减去这一层碰撞
static func sub_layer(node: CollisionObject2D, layer: int):
	node.collision_layer -= (node.collision_layer & layer)

##  减去这一层掩码
static func sub_mask(node: CollisionObject2D, mask: int):
	node.collision_mask -= (node.collision_mask & mask)

##  加上这一层碰撞
static func add_layer(node: CollisionObject2D, layer: int):
	node.collision_layer |= layer

##  加上这一层掩码
static func add_mask(node: CollisionObject2D, mask: int):
	node.collision_mask |= mask


